<?php

/* ========================================================================= */
/* This file is part of the HypnoPHP Framework Library                       */
/* Copyright (c) 2009-2010, Radu Buzea                                       */
/* ========================================================================= */

/**
 @author Radu Buzea
 @copyright Copyright (c) 2009-2010, Radu Buzea (www.hypnosis.ro)
 @license GNU Lesser General Public License (www.gnu.org/copyleft/lesser.html)
 @package MySQL Module for HypnoPHP
*/

// ============================================================================
// Class: Module_MySQL_Result
// ============================================================================

class Module_MySQL_Result extends HBaseDbResult
{

	/**
	* Check if a column (field) exists in the database result
	*
	* @param string $column_name
	* @return bool
	*/
	public function columnExists($column_name = '')
	{
		$column_name = trim($column_name);
		if ($column_name == '') { return false; }
		$col_names = $this->columnNames();
		return (is_array($col_names) && isset($col_names[$column_name]));
	}

	# ###########################################################################

	/**
	* Get the list of column names
	*
	* @param bool $include_table_name (optiona) Include table name
	* @return array
	*/
	public function columnNames($include_table_name = false)
	{
		if (count($this->__cols) == 0) { $this->columns(); }
		if (!is_array($this->__cols)) { return null; }
		$result = array();
		foreach ($this->__cols as $col_object) {
			if ($include_table_name) {
				$result[$col_object->name] = $col_object->table . '.' . $col_object->name;
			} else {
				$result[$col_object->name] = $col_object->name;
			}
		}
		return $result;
	}

	# ###########################################################################

	/**
	* Get the list of column objects
	*
	* @return array
	*/
	public function columns()
	{
		if (!$this->isReady()) { return null; }
		if (count($this->__rows) == 0) { $this->rows(); }
		if (!is_array($this->__rows)) { return null; }
		$row_data = $this->__rows[0];
		$cols_count = count($row_data);
		$result = array();
		for ($col = 0; $col < $cols_count; $col++) {
			$col_item = $this->database_object->fetchField($this->__database_result, $col);
			$col_item->length = intval(mysql_field_len($this->__database_result, $col));
			$col_item->flags = mysql_fieldFlags($this->__database_result, $col);
			$result[] = $col_item;
		}
		$this->__cols = $result;
		return $result;
	}

	# ###########################################################################

	/**
	* Returns the number of columns
	*
	* @return integer
	*/
	public function countColumns()
	{
		if (count($this->__cols) == 0) { $this->columns(); }
		if (!is_array($this->__cols)) { return 0; }
		return count($this->__cols);
	}

	# ###########################################################################

	/**
	* Returns the number of rows in the database result
	*
	* @return integer
	*/
	public function countRows()
	{
		if (count($this->__rows) == 0) { $this->rows(); }
		if (!is_array($this->__rows)) { return 0; }
		return count($this->__rows);
	}

	# ###########################################################################

	/**
	* Get the current row data
	*
	* @param bool $assoc Associate column names as keys in the row array result
	* @return array
	*/
	public function row($assoc = false)
	{
		if (!$this->isReady()) { return null; }
		if ($assoc) { return $this->database_object->fetchAssoc($this->__database_result); }
		return $this->database_object->fetchRow($this->__database_result);
	}

	# ###########################################################################

	/**
	* Return a list of rows data from the database result
	*
	* @param bool $assoc Associate column names as keys in the row array result
	* @return array
	*/
	public function rows($assoc = false)
	{
		if (!$this->isReady()) { return null; }
		$result = array();
		while ($row = $this->row($assoc)) {
			$result[] = $row_result;
		}
		$this->__rows = $result;
		return $result;
	}

	# ###########################################################################

}
